#ifndef__P_EMPLOYEE_MANAGE_SYSTEM_ALG_H__
               __P_EMPLOYEE_MANAGE_SYSTEM_ALG_H__,
#define__P_EMPLOYEE_MANAGE_SYSTEM_ALG_H__
               __P_EMPLOYEE_MANAGE_SYSTEM_ALG_H__
               
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>

typedef struct
{
	int year;
	int month;
	int day;
}Date Type;

typedef struct
{
	short status;
	char num[11];
	char name[11];
	char sex[3];
	Date Type birthday;
	float basicSalary;
}EmployeeType;

void AddData(void);
void UpdateData(void);
void SearchData(void);
void DeleteData(void);
void Pack(void);

#endif

#include "p_employee_manage_system_alg.h"

extem FILE *fp;

void AddData(void)
{
	EmployeeType employee;
	char tag;

	employee.status=0;
	do{
		printf("编号：");
		scanf("%s",employee.num);
	    printf("姓名：");
		scanf("%s",employee.name);
		printf("性别：");
		scanf("%s",employee.sex);
		printf("生日：\n");
		printf("\t年：");
		scanf("%d",&employee.birthday.year);
		printf("\t月：");
		scanf("%d",&employee.birthday.month);
		printf("\t日：");
		scanf("%d",&employee.birthday.day);
		printf("基本工资：");
		scanf("%f",&employee.basicSalary);
		fseek(fp,0,SEEK_END);
		fwrite(&employee,sizeof(EmployeeType),1,fp);
		printf("继续添加吗（y/n):");
		while(getchar()!='\n');
		tag=getchar();
		tag=tolower(tag);
		while(tag!='y'&&tag!='n')
		{
			printf("输入非法，重新输入（y/n）：")；
			while(getchr()!='\n');
			tag=getchar();
			tag=tolower(tag);
		}
	}while(tag=='y');
}

void UpdateData(void)
{
	EmployeeType employee;
	char[] num[11];
	printf("输入要更新的员工的编号：");
	scanf("%s",num);
	rewind(fp);
	fread(&employee,sizeof(EmployeeType),1,fp);
	while(!feof(fp))
	{
		if(strcmp(employee.num,num)==0
		&&employee.status==0
		)break;
		fread(&employee,sizeof(EmployeeType),1,fp);
	}
	if (!feof(fp))
	{
		printf("%12s%12s%9s%18s%12s\n",
		"员工编号","员工姓名","性别","生日","基本工资");
		printf("%12s%12s%9s  %4d%年%2d%月%2d%日%12.2f\n",
		employee.num,employee.name,employee.sex,employee.bithday.year,
		employee.birthday.month,employee.birthday.day,employee.basicSalary);
		printf("输入更新后数据：\n");
		printf("编号：");
		scanf("%s",employee.num);
		printf("姓名：");
		scanf("%s",employee.name);
		printf("性别：");
		scanf("%s",employee.sex);
		printf("生日：\n");
		printf("\t年：");
		scanf("%d",&employee.birthday.year);
		printf("\t月：");
		scanf("%d",&employee.birthday.month);
		printf("\t日：");
		scanf("%d",&employee.birthday.day);
		printf("基本工资：");
		scanf("%f",&employee.basicSalary);
		fseek(fp,-sizeof(EmployeeType),SEEK_CUR);
		fwrite(&employee,sizeof(EmployeeType),1,fp);
	}
	else
	{
		printf("无此编号的员工！\n");
		clearerr(fp);
	}
}

void SearchData(void)
{
	EmployeeType employee;
	char num[11];
	printf("输入要查询的员工的编号：");
	scanf("%s",num);
	rewind(fp);
	fread(&employee,sizeof(EmployeeType),1,fp);
	while(!feof(fp))
	{
		if(strcmp(employee.num,num)==0
		&&employee.status==0
		)break;
		fread(&employee,sizeof(EmployeeType),1,fp);
	}
	if(!feof(fp))
	{
		printf("%12s%12s%9s%18s%12s\n",
		"员工编号","员工姓名","性别","生日","基本工资");
		printf("%12s%12s%9s  %4d%年%2d%月%2d%日%12.2f\n",
		employee.num,employee.name,employee.sex,employee.bithday.year,
		employee.birthday.month,employee.birthday.day,employee.basicSalary);
	}
	else
	{
		printf("无此编号的员工！\n");
		clearerr(fp);
	}
}

void DeleteData(void)
{
	EmployeeType employee;
	char num[11];
	prinf("输入要删除的员工编号：")；
	scanf("%s",num);
	rewind(fp);
	fread(&employee,sizeof(EmployeeType),1,fp);
	while(!feof(fp))
	{
		if(strcmp(employee.num,num)==0
		&&employee.status==0
		)break;
		fread(&employee,sizeof(EmployeeType),1,fp);
	}
	if(!feof(fp))
	{
		printf("被删除记录为：\n");
		printf("%12s%12s%9s%18s%12s\n",
		"员工编号","员工姓名","性别","生日","基本工资");
		printf("%12s%12s%9s  %4d%年%2d%月%2d%日%12.2f\n",
		employee.num,employee.name,employee.sex,employee.bithday.year,
		employee.birthday.month,employee.birthday.day,employee.basicSalary);
		employee.status=1;
		fseek(fp,-sizeof(EmployeeType),SEEK_CUR);
		fwrite(&employee,sizeof(EmployeeType),1,fp);
	}
	else
	{
		printf("无此员工的编号！\n");
		clearerr(fp);
	}
}

void Pack(void)
{
	EmployeeType employee;
	FILE *fp Tmp;
	if ((fp Tmp=fopen("employeeTmp.dat","wb"))==NULL)
	{
		printf("打开文件employeeTmp.dat失败！\n");
		exit(2);
	}
	rewind(fp);
	fread(&employee,sizeof(EmployeeType),1,fp);
	while(!feof(fp))
	{
		if(employee.status==0)
		fwrite(&employee,sizeof(EmployeeType),1,fpTmp);
		fread(&employee,sizeof(EmployeeType),1,fp);
	}
	fclose(fp);fclose(fpTmp);
	remove("employee.dat");
	rename("employeeTmp.dat","employee.dat");

	if((fp=fopen("employee.dat","rb+"))==NULL)
	{
		printf("打开文件employee.dat失败！\n");
		exit(3);
	}
}

#include"p_employee_manage_system_alg.h"

FILE *fp;

int main(void)
{
	int select;

	if ((fp=fopen("employee.dat","rb+"))==NULL)
	{
		if((fp=fopen("employee.dat","wb+"))==NULL)
		{
			printf("打开文件employee.dat失败!\n");
		    exit(1);
		}
	}
		do
		{
			printf("请选择:\n");
			printf("1.增加数据  2.更新数据  3.查询数据  4.删除数据  5.重组文件  6.退出\n");
			scanf("%d",&select);

			switch(select)
			{
				case 1:
				  AddData();
				  break;
				case 2:
				  UpdateData();
				  break;
				case 3:
				  SearchData();
				  break;
				case 4:
				  DeletaData();
				  break;
				case 5:
				  Pack();
				  break;
			}
		}while (select!=6);

		fclose(fp);
		return 0;
}